﻿using CacheManager.Core;
using DH.Core.Logger;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace DH.Core.Helper
{
   public class CacheHelper
    {
        private static ICacheManager<object> manager = null;
        public static ICacheManager<object> CustomManager
        {
            get
            {
                try {
                    if (manager == null)
                    {
                        manager = CacheFactory.Build("getStartedCache", settings =>
                        {
                            //settings.WithMicrosoftMemoryCacheHandle("handleName");//使用内存

                            //settings.WithMicrosoftMemoryCacheHandle("handleName") //使用内容加redis
                            //.And

                            settings                                               //只是用redis
                            .WithRedisConfiguration("redis", config =>
                            {
                                config.WithAllowAdmin()
                                    .WithDatabase(0)
                                    .WithEndpoint("localhost", 6379);
                            })
                            .WithMaxRetries(100)
                            .WithRetryTimeout(50)
                            .WithRedisBackplane("redis")
                            .WithJsonSerializer()   /*CacheManager.Redis.RedisCacheHandle 要求使用序列化的缓存值*/
                            .WithRedisCacheHandle("redis", true);

                        });
                    }
                    return manager;
                }
                catch (Exception ex)
                {

                    var _logger= LoggerProvider.GetLogger();
                    _logger.LogDebug("redis服务器打开失败：" + ex.Message);

                }
                return manager;
              

            }

        }
    }
}
